//
//  MBPHUD.m
//  QuanJieShopProject
//
//  Created by apple on 2018/1/30.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "MBPHUD.h"

//默认时间
#define Delay_Time 1.5f
static UIView *lastViewWithHUD = nil;

@implementation MBPHUD

+ (UIView *)rootView{
    UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC.view;
}
#pragma mark -- 显示文字,持续时间
+ (MBProgressHUD *)showAlertWithTitle:(NSString *)title duration:(float)duration{
    return [self showAlertWithTitle:title inView:nil duration:Delay_Time];
}
#pragma mark -- 显示文字
+ (MBProgressHUD *)showAlertWithTitle:(NSString *)title{
    return [self showAlertWithTitle:title duration:Delay_Time];
}
#pragma mark -- 显示在某个界面
+ (MBProgressHUD *)showAlertWithTitle:(NSString *)title inView:(UIView *)inView{
    return [self showAlertWithTitle:title inView:inView duration:Delay_Time];
}


#pragma mark -- 显示在某个界面,持续时间
+(MBProgressHUD *)showAlertWithTitle:(NSString *)title inView:(UIView *)inView duration:(float)duration{
    MBProgressHUD *hud = [self showBaseHUDWithTitle:title inView:inView];
    hud.mode = MBProgressHUDModeText;
    [hud hideAnimated:YES afterDelay:duration];
    return hud;
}



#pragma mark - 网络加载提示框
+ (MBProgressHUD *)showHUDWithTitle:(NSString *)title {
    return [self showHUDWithTitle:title inView:nil];
}

+ (MBProgressHUD *)showHUDWithTitle:(NSString *)title inView:(UIView *)inView{

    MBProgressHUD *hud = [self showBaseHUDWithTitle:title inView:inView];
    hud.mode = MBProgressHUDModeIndeterminate;
    return hud;
}

#pragma mark - 基础控件
+ (MBProgressHUD *)showBaseHUDWithTitle:(NSString *)title inView:(UIView *)inView{
    UIView *targetView = inView;
    if (targetView == nil) {
        targetView = [self rootView];
    }
    targetView = [UIApplication sharedApplication].keyWindow;
    lastViewWithHUD = targetView;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:targetView animated:YES];
    hud.detailsLabel.text = title;
    hud.removeFromSuperViewOnHide = YES;
    hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    hud.margin = 25;
    return hud;
}

#pragma mark -- 隐藏
+ (void)hideMBPHUD{
    [MBProgressHUD hideHUDForView:lastViewWithHUD animated:YES];
}

@end
